// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import mainManage from "../../framework/mainManage";
import Tool from "../../framework/Tool";
import gameData from "../gameData";
import { SDKManager } from "../SDKManager/SDKManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class randomUseSkin extends cc.Component {

    @property(cc.Node)
    skinNode : cc.Node = null;

    @property(cc.Node)
    cancelNode : cc.Node = null;

    @property(cc.Node)
    pieceNode : cc.Node = null;

    clickTimes = 2;

    curClickTimes = 0;

    randomSkin = null;

    initData = null;

    initPos = null;

    onLoad(){
        // this.scheduleOnce(()=>{
        //     this.cancelNode.active = true;
        // },2);
        let arr = [];
        for(let i = 0 ; i < 10 ; i++){
            if(gameData.getRoleProcessSkin()[i] == 0){
                arr.push(i + 1)
            }
        }
        if(arr.length == 0){
            this.randomSkin = (Math.random() * 10) | 0;
        }else{
            this.randomSkin = arr[Math.random() * arr.length | 0];
        }

        if(this.randomSkin < 10){
            cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                if(err){
                    console.log(err);
                    return
                }
                this.skinNode.getComponent(sp.Skeleton).skeletonData = res;
                let spk = this.skinNode.getComponent(sp.Skeleton);
                spk.setSkin("pf0" +  this.randomSkin);
                spk.setAnimation(0,"animation",true);
            });
        }else{

            if(this.randomSkin == 20){
                if(gameData.lastColorfulList[2] == 0){
                    cc.loader.loadRes("anim/zhujue22_1",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let spk = this.skinNode.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setAnimation(0,"animation",true);
                    })
                }else if(gameData.lastColorfulList[2] == 1){
                    cc.loader.loadRes("anim/zhujue22_2",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let spk = this.skinNode.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setAnimation(0,"animation",true);
                    })
                }
            }else if(this.randomSkin > 12){
                cc.loader.loadRes("anim/zhujue" + (this.randomSkin + 1),sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    this.skinNode.getComponent(sp.Skeleton).skeletonData = res;
                    let spk = this.skinNode.getComponent(sp.Skeleton);
                    if(this.randomSkin == 13){
                        spk.setSkin("pfi" + (gameData.lastColorfulPart + 1));
                    }else if(this.randomSkin == 15){
                        spk.setSkin("pf1");
                    }else if(this.randomSkin == 17){
                        spk.setSkin("pifu" + (gameData.lastColorfulList[1] + 1));
                    }
                    spk.setAnimation(0,"animation",true);
                })
            }else{
                cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    this.skinNode.getComponent(sp.Skeleton).skeletonData = res;
                    let spk = this.skinNode.getComponent(sp.Skeleton);
                    spk.setSkin("pf" +  this.randomSkin);
                    spk.setAnimation(0,"animation",true);
                });
            }
        }

        // if(gameData.colorfulPiece[0][2] == 0){
        //     this.node.on("touchstart",this.TouchStart,this);
        //     this.node.on("touchmove",this.TouchMove,this);
        //     this.node.on("touchend",this.TouchEnd,this);
        //     this.node.on("touchcancel",this.TouchEnd,this);
        //     this.node.getChildByName("use").on("touchstart",this.clickUse,this);
        //     this.node.getChildByName("use").on("touchend",this.noClickUse,this);
        //     this.node.getChildByName("use").on("touchcancel",this.noClickUse,this);
        // }else{
        //     this.node.getChildByName("use").getComponent(cc.Button).enabled = true;
        // }

        this.node.getChildByName("use").getComponent(cc.Button).enabled = true;
    }

    isClick = false;
    clickUse(event){
        this.isClick = true;
    }

    noClickUse(event){
        this.isClick = false;
    }

    btnClick(event,data){
        if(parseInt(data) == 1){

            let isSeeVideo = false;

            SDKManager.showRewardVideo("随机皮肤",()=>{

                console.log("获取随机皮肤");

                isSeeVideo = true;

                cc.game.emit("setGameRoleSkin",this.randomSkin);
                cc.director.getPhysicsManager().enabled = true;
                this.node.destroy();

            },()=>{
                if(isSeeVideo){
                    mainManage.createToast("Hadiahnya akan diberikan setelah menonton video ini");
                }else{
                    mainManage.createToast("Tidak ada iklan yang tersedia saat ini");
                }
            })
        }

        if(parseInt(data) == 2){
            //不了谢谢
            this.node.destroy();
            cc.director.getPhysicsManager().enabled = true;
        }
    }

    isClickArea = false;
    TouchStart(event){
        if(!this.isClick) return;
        this.isClickArea = true;
        this.initData = new Date().getTime();
        let pos  = this.node.convertToNodeSpaceAR(event.getLocation());
        this.initPos = cc.v2(pos.x,pos.y);
    }

    TouchMove(event){
        if(!this.isClick) return;
        let delay = (new Date().getTime() - this.initData) / 1000;
        let pos = this.node.convertToNodeSpaceAR(event.getLocation());
        if(delay > 2){
            let pos1 = pos.sub(this.initPos);
            let pos2 = cc.v2(this.node.getChildByName("use").x,this.node.getChildByName("use").y);
            let pos3 = pos2.add(pos1);
            this.node.getChildByName("use").x = pos3.x;
            this.node.getChildByName("use").y = pos3.y;
        }else{
            return;
        }
        this.initPos = pos;
    }

    TouchEnd(event){
        if(!this.isClick && !this.isClickArea) return;
        let delay = (new Date().getTime() - this.initData) / 1000;
        if(delay > 2){
            console.log("出现碎片弹框");
            gameData.colorfulPiece[0][2] = 1;
            gameData.saveColorfulPiece();
            Tool.loadPrefab("prefabs/getSkin",res=>{
                let view = cc.instantiate(res);
                if(gameData.getColorfulPieceNumber(gameData.colorfulPiece[0]) == 7){
                    view.getComponent("getSkinMediator").setCallBack(true,3);
                }else{
                    view.getComponent("getSkinMediator").setCallBack(false,3);
                }
                view.parent = cc.find("Canvas");
            });
        }else{

            cc.game.emit("setGameRoleSkin",this.randomSkin);
            cc.director.getPhysicsManager().enabled = true;
            this.node.destroy();

            console.log("播放视频");
        }
        this.isClickArea = false;
    }
}
